package com.lg.erp.controller.chenkang;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.erp.entity.JsonResult;
import com.lg.erp.entity.chenkang.TShelves;
import com.lg.erp.service.chenkang.IShelvesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;

import java.util.List;
import java.util.UUID;

@RestController
public class ShelvesController {

    @Autowired
    private IShelvesService shelvesService;

    //通过货架id来查询货区编号
    @RequestMapping("/shelvesIdByCargoAreaDamId")
    public JsonResult selectShelvesIdByCargoAreaId() {
        List<TShelves> tShelves = shelvesService.selectShelvesIdByCargoAreaId();
        System.out.println("结果"+tShelves);
        return new JsonResult(1,"查询成功",tShelves);
    }


    //通过货架id查找货区名称的方法
    @RequestMapping("/shelvesIdByCargoAreaName")
    public JsonResult selectShelvesIdByCargoAreaName() {
        List<TShelves> tShelves = shelvesService.selectShelvesIdByCargoAreaName();
        return new JsonResult(1,"查询成功",tShelves);
    }

    //分页(模糊查询)
    @RequestMapping("/shelvesPage")
    public JsonResult pageShelves(@RequestParam(value = "current",required = false,defaultValue = "1") Integer current,String shelvesNo,String creatTime){
        Page<TShelves> page=new Page<>(current,10);
        System.out.println("来了");
        System.out.println(current + "--" + shelvesNo + "--" + creatTime);
        IPage<TShelves> pages=shelvesService.pageShelves(page,shelvesNo,creatTime);
        return new JsonResult(1,"ok",pages);
//        return new JsonResult(1,"ok",null);
    }


    //增加
    @PostMapping("/shelves")
    public JsonResult saveShelves(@RequestBody TShelves shelves){
        shelves.setId(UUID.randomUUID().toString().substring(0,7));
        shelves.setCreatTime(new Timestamp(System.currentTimeMillis()));
        shelvesService.save(shelves);
        return new JsonResult(1,"创建成功",shelves);
    }
    //修改
    @PutMapping("/shelves")
    public JsonResult updateShelves(@RequestBody TShelves shelves){
        shelvesService.updateById(shelves);
        return new JsonResult(1,"更新成功",shelves);

    }
    @RequestMapping(value = "/shelves/{id}",method = RequestMethod.DELETE)
    public JsonResult removeShelves(@PathVariable("id") String id){
        shelvesService.removeById(id);
        return new JsonResult(1,"删除成功",id);
    }
}
